const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
    host: process.env.DB_HOST || "43.143.253.188",
    port: parseInt(process.env.DB_PORT || "3306", 10),
    user: process.env.DB_USER || "nextjs",
    password: process.env.DB_PASS || "yEAiRE2jcxTyRBy5",
    database: process.env.DB_NAME || "nextjs",
    charset: 'utf8mb4',
    timezone: '+08:00',
};

async function verifyMountData() {
    let connection;

    try {
        console.log('🔍 验证坐骑基础数据...');

        // 连接数据库
        connection = await mysql.createConnection(dbConfig);
        console.log('✅ 数据库连接成功！');

        // 查询所有坐骑数据
        const [mounts] = await connection.execute(`
      SELECT 
        id, name, descr, img, type, price,
        ap, apIni, hp, hpIni, mp, mpIni, sp, spIni,
        created_at
      FROM mount_base 
      ORDER BY id ASC
    `);

        console.log(`📊 共找到 ${mounts.length} 条坐骑数据\n`);

        // 按类型分组显示
        const typeNames = { 1: '战马', 2: '名马', 3: '神马' };
        const groupedMounts = mounts.reduce((acc, mount) => {
            const typeName = typeNames[mount.type] || '未知';
            if (!acc[typeName]) acc[typeName] = [];
            acc[typeName].push(mount);
            return acc;
        }, {});

        // 显示详细数据
        for (const [typeName, typeMounts] of Object.entries(groupedMounts)) {
            console.log(`🏇 ${typeName} (${typeMounts.length}匹):`);
            console.log('='.repeat(60));

            typeMounts.forEach(mount => {
                console.log(`\n📋 ${mount.id}. ${mount.name}`);
                console.log(`   🖼️  图片: ${mount.img}`);
                console.log(`   📝 描述: ${mount.descr}`);
                console.log(`   💰 价格: ${mount.price} 银币`);
                console.log(`   ⚔️  攻击力: ${mount.ap} (初始: ${mount.apIni})`);
                console.log(`   ❤️  生命值: ${mount.hp} (初始: ${mount.hpIni})`);
                console.log(`   🔮 法力值: ${mount.mp} (初始: ${mount.mpIni})`);
                console.log(`   🏃 速度: ${mount.sp} (初始: ${mount.spIni})`);
                console.log(`   📅 创建时间: ${mount.created_at}`);
            });
            console.log('');
        }

        // 统计信息
        console.log('📈 数据统计:');
        console.log('='.repeat(40));
        Object.entries(groupedMounts).forEach(([typeName, typeMounts]) => {
            console.log(`${typeName}: ${typeMounts.length} 匹`);
        });

        // 属性统计
        const totalAp = mounts.filter(m => m.ap > 0).length;
        const totalHp = mounts.filter(m => m.hp > 0).length;
        const totalMp = mounts.filter(m => m.mp > 0).length;
        const totalSp = mounts.filter(m => m.sp > 0).length;

        console.log('\n🎯 属性分布:');
        console.log(`攻击型坐骑: ${totalAp} 匹`);
        console.log(`生命型坐骑: ${totalHp} 匹`);
        console.log(`法力型坐骑: ${totalMp} 匹`);
        console.log(`速度型坐骑: ${totalSp} 匹`);

        console.log('\n✅ 坐骑数据验证完成！');

    } catch (error) {
        console.error('❌ 验证过程中发生错误:', error);
        throw error;
    } finally {
        if (connection) {
            await connection.end();
            console.log('🔌 数据库连接已关闭');
        }
    }
}

// 执行验证
if (require.main === module) {
    verifyMountData()
        .then(() => {
            console.log('✅ 验证完成');
            process.exit(0);
        })
        .catch((error) => {
            console.error('❌ 验证失败:', error);
            process.exit(1);
        });
}

module.exports = { verifyMountData };
